﻿/*
   Author: Tomer Paz
   games.com
   2009
*/

/* Pre-Dies Actions Dialog
	This dialog will ask player to Sell property stating offered Price
*/



package monopoly.GameController.Dialogs {
	import fl.controls.*;
	//import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.events.TimerEvent;
	import flash.events.EventDispatcher;
	import fl.controls.TextInput;
	import fl.controls.ComboBox;
	import fl.containers.ScrollPane;
	import fl.controls.Button;
	import fl.data.DataProvider;
	import flash.text.TextFieldAutoSize;

	import monopoly.GameController.Dialogs.IDialog;
	import monopoly.GameController.Dialogs.Templates.*;
	import monopoly.GameController.PopupMessageHandler;
	import monopoly.Field.Field;
	import monopoly.Player.Player;
	import monopoly.Player.PlayersCollection;
	import monopoly.IFS.GLOBALS;
	import monopoly.Localization.*;
	
	public class SellDialog extends ScrollPane implements IDialog
		{
		public static const PLAYER_ID  = 0;
		public static const FIELD	   = 1;
		public static const SELL_PRICE = 2;
		
		protected var _priceLabel:Label = new Label();
		protected var _buyerLabel:Label = new Label();
		protected var _inputText:TextInput = new TextInput();
		protected var _comBox:ComboBox = new ComboBox();
		protected var _OKButton:Button = new Button();
		//protected var _mainPane:ScrollPane = new ScrollPane();
		protected var _data:Object = new Object();
		protected var _myStrings:ILocalLanguage = LocalLanguage.getInstance();
		
		protected var _players:PlayersCollection;
		protected var _MyPlayerID:int;
		
		// for IDialog compatibility only
		protected var _chosenAction:uint;
		
		protected var VerticalPadding:Number = GLOBALS.VPADDING + 5;
		
		public function SellDialog(field:Field, players:PlayersCollection, myPlayerID:int)
			{
			_players 		= players;
			_MyPlayerID		= myPlayerID;
			_data [ FIELD ] = field;
			
			setupComponents();
			}
		
		// for IDilaog compatilibitly only
		public function get chosenAction():uint
			{
			return 0;
			}
		public function get Data():Object
			{
			return _data;
			}
			
		protected function setupComponents():void
			{
			this.visible = false;
			
			with (this)
				{
				setSize(180, 200);
				x = 0;
				y = 0;
				alpha = 0.9;
				//opaqueBackground = GLOBALS.DEFAULT_DIALOG_COLOR;
				}

			var _field:Field = _data[ FIELD ];
			
			with (_priceLabel)
				{
				move(5, 5);
				text = _myStrings.DlgSellText(_field.PropertyName, _field.Cost);
				autoSize = TextFieldAutoSize.LEFT;
				}
			addChild(_priceLabel);
			
			// price text
			_inputText.maxChars 			= 8;
			_inputText.textField.background = true;
			_inputText.textField.border	   	= true;
			_inputText.text 				= String(_field.Cost);
			_inputText.restrict				= "0123456789";
			_inputText.drawFocus(true);
			_inputText.setSize(this.width * 0.8, this.height*0.1);
			_inputText.move(5, _priceLabel.y + _priceLabel.height + VerticalPadding);
			addChild(_inputText);
			
			// choose-user combobox
			add_players_to_cbox();
			_comBox.labelFunction = player_name;
			_comBox.move(5, _inputText.y + _inputText.height + VerticalPadding);
			_comBox.dropdownWidth = 120;
			_comBox.dropdown.iconField = null;
			addChild(_comBox);
			
			_OKButton.addEventListener(MouseEvent.CLICK, buttonClick);
			_OKButton.move(5, _comBox.y + _comBox.height + VerticalPadding);
			_OKButton.label = "OK";
			addChild(_OKButton);

			this.visible = true;
			}
			
		protected function add_players_to_cbox():void
			{
			var playerIter = _players.iterator();
			var player:Player;
			for (; playerIter.hasNext() && (player = Player(playerIter.next()));)
				{
				if (_MyPlayerID != player.ID)
					{
					_comBox.addItem(player);
					}
				}
			}
		protected function player_name(player:Object):String
			{
			return ( (null == player) ? _myStrings.DlgSellPOption : (player as Player).Name );
			}
		
		protected function buttonClick(e:MouseEvent):void
			{
			/* validity check  - not yet implemented */
			// we might want to add more Content level filtering for values, like Ranges, but for now only check syntax
			
			_data [ SELL_PRICE ] = int(_inputText.text);
			_data [ PLAYER_ID ]	 = (-1 != _comBox.selectedIndex) ? (_comBox.selectedItem as Player).ID : _comBox.getItemAt(0);
				
			/* dialog done */	
			
			this.removeEventListener(MouseEvent.CLICK, buttonClick);
			with (this)
				{
				removeChild(_priceLabel);
				removeChild(_inputText);
				removeChild(_OKButton);
				}
				
			dispatchEvent(new Event(Event.COMPLETE));
			}
		}
}